package dialogs;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;

import javax.swing.*;

import awesomeDocumentIO.AwesomeDocumentImporter;
/**
 * This dialog is used to create a new document type.
 * @author Brittin
 * Source originally from:
 * http://java.sun.com/docs/books/tutorial/uiswing/examples/components/DialogDemoProject/src/components/CustomDialog.java
 */
public class NewDocumentTypeDialog extends JDialog implements ActionListener,
			PropertyChangeListener {
		private String[] typedText;
		private JTextField labelTextField;
		private JTextField extensionTextField;
		private JOptionPane optionPane;
		private HashMap<String, String> docinfo; 
		private String btnString1 = "Enter";
		private String btnString2 = "Cancel";

		/**
		 * Returns null if the typed string was invalid; otherwise, returns the
		 * string as the user entered it.
		 */
		public String[] getValidatedText() {
			return typedText;
		}

		/** Creates the reusable dialog. */
		public NewDocumentTypeDialog(Frame aFrame) {
			super(aFrame, true);
			docinfo = AwesomeDocumentImporter.getAllDocInfo();
			setTitle("Create New Document Type");

			labelTextField = new JTextField(10);
			extensionTextField = new JTextField(10);
			// Create an array of the text and components to be displayed.
			String msgString1 = "Please enter the document type name:";
			String msgString2 = "Please enter the associated extension\n without the period:";
			Object[] array = { msgString1, labelTextField, msgString2, extensionTextField };

			// Create an array specifying the number of dialog buttons
			// and their text.
			Object[] options = { btnString1, btnString2 };

			// Create the JOptionPane.
			optionPane = new JOptionPane(array, JOptionPane.PLAIN_MESSAGE,
					JOptionPane.YES_NO_OPTION, null, options, options[0]);

			// Make this dialog display it.
			setContentPane(optionPane);

			// Handle window closing correctly.
			setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent we) {
					/*
					 * Instead of directly closing the window, we're going to
					 * change the JOptionPane's value property.
					 */
					optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
				}
			});

			// Ensure the text field always gets the first focus.
			addComponentListener(new ComponentAdapter() {
				public void componentShown(ComponentEvent ce) {
					labelTextField.requestFocusInWindow();
				}
			});

			// Register an event handler that puts the text into the option
			// pane.
			labelTextField.addActionListener(this);

			// Register an event handler that reacts to option pane state
			// changes.
			optionPane.addPropertyChangeListener(this);
			pack();
		}

		/** This method handles events for the text field. */
		public void actionPerformed(ActionEvent e) {
			optionPane.setValue(btnString1);
		}

		/** This method reacts to state changes in the option pane. */
		public void propertyChange(PropertyChangeEvent e) {
			String prop = e.getPropertyName();

			if (isVisible()
					&& (e.getSource() == optionPane)
					&& (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY
							.equals(prop))) {
				Object value = optionPane.getValue();

				if (value == JOptionPane.UNINITIALIZED_VALUE) {
					// ignore reset
					return;
				}

				// Reset the JOptionPane's value.
				// If you don't do this, then if the user
				// presses the same button next time, no
				// property change event will be fired.
				optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);

				if (btnString1.equals(value)) {
					String labelText = labelTextField.getText().trim();
					String extText = extensionTextField.getText().trim();
					
					if (!"".equals(labelText) && !"".equals(extText)) {
						// make sure that extension does no have a period in it
						if(extText.contains(".")) {
							labelTextField.selectAll();
							JOptionPane.showMessageDialog(NewDocumentTypeDialog.this,
									"Extension should not include the period.",
									"Please try again", JOptionPane.ERROR_MESSAGE);
							typedText = null;
							labelTextField.requestFocusInWindow();
						}
						
						
						
						// Check if this label is already used
						if (this.docinfo.containsKey(labelText) || this.docinfo.containsValue(extText) ) {
							labelTextField.selectAll();
							JOptionPane.showMessageDialog(NewDocumentTypeDialog.this,
									"This document type already exists. Please enter a unique label and extension.",
									"Please try again", JOptionPane.ERROR_MESSAGE);
							typedText = null;
							labelTextField.requestFocusInWindow();
						} else {
							typedText = new String[2];
							typedText[0] = labelText;
							typedText[1] = extText;
							clearAndHide();	
						}
					} else {
						// text was invalid
						labelTextField.selectAll();
						JOptionPane.showMessageDialog(NewDocumentTypeDialog.this,
								"Both fields must contain some text.",
								"Please try again", JOptionPane.ERROR_MESSAGE);
						typedText = null;
						labelTextField.requestFocusInWindow();
					}
				} else { // user closed dialog or clicked cancel
					typedText = null;
					clearAndHide();
				}
			}
		}

		/** This method clears the dialog and hides it. */
		public void clearAndHide() {
			labelTextField.setText(null);
			setVisible(false);
		}
	}
